package com.fulihui.integral.core.util;

import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


public final class RedisUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);

    private RedisUtil() {}

    public static String get(JedisPool jedisPool, String key) {
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        String value = null;
        try (Jedis jedis = jedisPool.getResource()) {
            value = jedis.get(key);
        }catch (Exception e){
            LOGGER.error("redis get :{}, error : {}", key, e);
        }

        return value;
    }

    public static boolean set(JedisPool jedisPool, String key, String value) {
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        Preconditions.checkNotNull(value, "key must not null!");
        boolean result = true;
        try (Jedis jedis = jedisPool.getResource()){
            jedis.set(key, value);
        }catch (Exception e){
            result = false;
            LOGGER.error("redis set :{}, error : {}", key, e);
        }
        return result;
    }

    public static Long setnx(JedisPool jedisPool, String key, String value) {
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        Preconditions.checkNotNull(value, "key must not null!");
        Long result = null;
        try (Jedis jedis = jedisPool.getResource()){
            result = jedis.setnx(key, value);
        }catch (Exception e){
            LOGGER.error("redis setnx :{}, error : {}", key, e);
        }
        return result;
    }


    public static Long incr(JedisPool jedisPool, String key) {
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        Long result = null;
        try (Jedis jedis = jedisPool.getResource()){
            result = jedis.incr(key);
        }catch (Exception e){
            LOGGER.error("redis incr :{}, error : {}", key, e);
        }
        return result;
    }

    public static Long incrBy(JedisPool jedisPool, String key, Long value) {
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        Preconditions.checkNotNull(value, "value must not null!");
        Long result = null;
        try (Jedis jedis = jedisPool.getResource()){
            result = jedis.incrBy(key, value);
        }catch (Exception e){
            LOGGER.error("redis incrBy :{}, error : {}", key, e);
        }
        return result;
    }


    public static boolean exists(JedisPool jedisPool, String key){
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        boolean result = false;
        try (Jedis jedis = jedisPool.getResource()){
            result = jedis.exists(key);
        }catch (Exception e){
            LOGGER.error("redis exists :{}, error : {}", key, e);
        }
        return result;
    }

    public static Long del(JedisPool jedisPool, String key){
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        Long value = null;
        try (Jedis jedis = jedisPool.getResource()){
            value = jedis.del(key);
        }catch (Exception e){
            LOGGER.error("redis del :{}, error : {}", key, e);
        }

        return value;
    }

    public static Long expire(JedisPool jedisPool, String key, int second){
        Preconditions.checkNotNull(jedisPool, "jedisPool must not null!");
        Preconditions.checkNotNull(key, "key must not null!");
        Long value = null;
        try (Jedis jedis = jedisPool.getResource()){
            value = jedis.expire(key, second);
        }catch (Exception e){
            LOGGER.error("redis expire :{}, error : {}", key, e);
        }

        return value;
    }
}
